package com.dahuyou.monitor.method;

import net.bytebuddy.implementation.bind.annotation.AllArguments;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
 * byte buddy 执行代理类
 */
public class MyByteBuddyDelegator {

    @RuntimeType
    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable, @AllArguments Object[] args) throws Exception {
        long start = System.currentTimeMillis();
        try {
            // 执行原方法逻辑
            return callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("方法： " + method + ", 执行耗时：" + (System.currentTimeMillis() - start) + " 毫秒");
        }
        return null;
    }
}
